package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.qo.JSONResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private EmployeeMapper employeeMapper;

    //查询部门请求
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") EmployeeQueryObject qo){

        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageResult", pageInfo);

        List<Employee> employees = employeeService.listALL();
        model.addAttribute("employees", employees);

        model.addAttribute("departments", departmentService.listALL());

        model.addAttribute("user", UserContext.getCurrentUser());
        return "employee/list";
    }
    //处理删除部门请求
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){

        try {

            employeeService.delete(id);
            return new JSONResult();

        } catch(Exception e) {
            e.printStackTrace();

            return new JSONResult(false,"警告");
        }
    }

    //处理新增部门信息
    @RequestMapping("/input")
    @RequiredPermission(name="添加员工",expression = "employee.input")
    public String input(Long id,Model model){

        List<Department> departments = departmentService.listALL();
        model.addAttribute("departments", departments);
        model.addAttribute("roles", roleService.listALL());
        if (id != null) {
            Employee employee = employeeService.get(id);
            model.addAttribute("employee", employee);

        }

        return "employee/input";
    }

    //处理新增和修改页面
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiredPermission(name="员工新增或修改",expression = "employee.saveOrUpdate")
    public JSONResult saveOrUpdate(Employee employee,Long[] ids){

        if (employee.getId() != null) {

            employeeService.update(employee,ids);
        }else {
            employeeService.save(employee,ids);

        }
       return new JSONResult();
    }


    //修改密码
    @RequestMapping("/change")
    @ResponseBody
    public JSONResult change(String oldPassword,String newPassword){

        /*if (employee.getId()!=null){

        }*/

        try {
            employeeService.updateAll(oldPassword, newPassword);

            return new JSONResult();
        } catch(Exception e) {
            e.printStackTrace();

            return new JSONResult(false,e.getMessage());
        }


    }

    @RequestMapping("/inputUp")
    public String inputUp(){

        return "/employee/updatePwd";
    }


    @RequestMapping("/grater")
    public String grater(Employee employee,Model model){


        Employee employee1 = employeeService.get(employee.getId());
        model.addAttribute("employee1", employee1);
        return "/employee/resetPwd";
    }

    @RequestMapping("/resetPwd")
    @ResponseBody
    public JSONResult resetPwd(String newPassword,Long id){

        try {

            Employee employee = employeeMapper.selectOne(id);
            employee.setPassword(newPassword);
            employeeService.insert1(employee);
            return new JSONResult();
        } catch(Exception e) {
            e.printStackTrace();

            return new JSONResult(false,e.getMessage());
        }

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public JSONResult batchDelete(Long[] ids){

        employeeService.batchDelete(ids);

        return new JSONResult();
    }


    //后台检查用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(String name,Long id){
        System.out.println("===========================================================================");
        System.out.println(id);
        System.out.println(name);
        System.out.println("-----------------------------------------------------------------------------------");
        Employee employee = employeeService.selectByName(name,id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("valid",employee==null);
        return map;
    }



    //导出
    @RequestMapping("/exportXls")

    public void exportXls(HttpServletResponse response) throws IOException {

        //设置文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");


        //在业务层创建workbook
        Workbook wb = employeeService.exportXLs();


        //把excel的数据输出到浏览器的响应流中
        wb.write(response.getOutputStream());

    }


    /**
     *
     * 导入
     * @return
     */
    @RequestMapping("/importXls")
    @ResponseBody
    public JSONResult importXls(MultipartFile file) throws IOException {



        employeeService.importXLs(file);
        return new JSONResult();
    }

    @RequestMapping("/changeWord")
    @ResponseBody
    public JSONResult changeWord(Long id){


        Employee employee = employeeService.get(id);
        employee.setStatus(!(employee.isStatus()));

        employeeService.updateOne(employee);

        return new JSONResult();
    }


}
